package com.wangtao.firstapp.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wangtao.firstapp.R;

/**
 * 系统设置页面
 * Created by lanouhn on 16/9/19.
 */
public class SystemSetUpActivity extends AppCompatActivity {
    private ImageView iv_back_titlebar_systemsetup;// 标题返回按钮
    private TextView tv_contribute_systemsetup;// 投稿按钮
    private RelativeLayout achievement;// 个人成就
    private RelativeLayout interestin;// 编辑兴趣
    private RelativeLayout shareapp;// 分享应用
    private RelativeLayout cleancache;// 清理缓存
    private RelativeLayout feedback;// 意见反馈
    private RelativeLayout commonproblems;// 常见问题
    private RelativeLayout joinhalfsugar;// 加入半糖
    private RelativeLayout checknewversion;// 版本检测
    private CheckBox pushsettings;// 推送设置
    private CheckBox nightmodes;// 夜间模式
    private RelativeLayout taobaofastlogin;// 淘宝快捷登录
    private RelativeLayout logout;// 退出当前账号
    private TextView tv_cache;// 缓存数
    private LinearLayout sina, wechat, wechat_quan, qqzone, qqfriend, copylink;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_systemsetup);
        initView();
        initData();
        initEvent();
    }


    private void initView() {
        iv_back_titlebar_systemsetup = (ImageView) findViewById(R.id.iv_back_titlebar_systemsetup);// 标题栏返回按钮
        interestin = (RelativeLayout) findViewById(R.id.interestin);// 编辑兴趣
        shareapp = (RelativeLayout) findViewById(R.id.shareapp);// 分享应用
        cleancache = (RelativeLayout) findViewById(R.id.cleancache);// 清理缓存
        feedback = (RelativeLayout) findViewById(R.id.feedback);// 意见反馈
        checknewversion = (RelativeLayout) findViewById(R.id.checknewversion);// 版本检测
        tv_cache = (TextView) findViewById(R.id.tv_cache);// 缓存数
    }

    private void initData() {
        // 随机生成缓存数
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        double cachecount = Math.random() * 20;
        tv_cache.setText(df.format(cachecount) + " M");

    }

    private void initEvent() {
        // 返回按钮
        iv_back_titlebar_systemsetup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 编辑兴趣
        interestin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_interestin = new Intent(SystemSetUpActivity.this, MyInterestConfigActivity.class);
                startActivity(intent_interestin);
            }
        });
        // 分享应用
        shareapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });

        // 清理缓存
        cleancache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_cache.setText("0 M");
                Toast.makeText(SystemSetUpActivity.this, "清理成功", Toast.LENGTH_SHORT).show();
            }
        });

        // 意见反馈
        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_feedback = new Intent(SystemSetUpActivity.this, FeedBackActivity.class);
                startActivity(intent_feedback);
            }
        });
        // 版本检测
        checknewversion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SystemSetUpActivity.this, "已经是最新版本", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 弹出PopupWindow
    private void show() {
        // View view = LayoutInflater.from(SystemSetUpActivity.this).inflate(R.layout.shareapp_dialog, null);
        // AlertDialog.Builder ab = new AlertDialog.Builder(SystemSetUpActivity.this);
        final Dialog dialog = new Dialog(SystemSetUpActivity.this, R.style.my_dialog);
        RelativeLayout rootview = (RelativeLayout) LayoutInflater.from(SystemSetUpActivity.this).inflate(R.layout.shareapp_dialog, null);
        // 新浪微博
        rootview.findViewById(R.id.sina).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // 微信
        rootview.findViewById(R.id.wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SystemSetUpActivity.this, "您还没有安装微信哦", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        // 朋友圈
        rootview.findViewById(R.id.wechat_quan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SystemSetUpActivity.this, "您还没有安装微信哦", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        // QQ空间
        rootview.findViewById(R.id.qqzone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // QQ好友
        rootview.findViewById(R.id.qqfriend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // 复制链接
        rootview.findViewById(R.id.copylink).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SystemSetUpActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        dialog.setContentView(rootview);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
//      lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度
//      lp.alpha = 9f; // 透明度
        rootview.measure(0, 0);
        lp.height = rootview.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

}
